package com.ziytek.web.citizen.util;

import com.ziytek.web.citizen.config.ConfigInit;
import com.ziytek.web.citizen.constant.ServerConstant;
import com.ziytek.web.citizen.service.itf.BootNettyTcpServer;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @className SpringBeanUtils
 * @description: 描述
 * @jdkVersion: 1.8
 * @author: fhx
 * @date: 2024/7/2 9:53
 */
@Slf4j
@Component
public class SpringBeanUtils implements ApplicationContextAware, CommandLineRunner {
    @Getter
    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringBeanUtils.applicationContext = applicationContext;
    }

    @Override
    public void run(String... args) throws Exception {
        if (applicationContext == null) {
            return;
        }

        ConfigInit configInit = applicationContext.getBean(ConfigInit.class);
        if (applicationContext.getBean(ServerConstant.TCP_SERVICE_BEAN) instanceof BootNettyTcpServer bts) {
            bts.bind(configInit);
            bts.bind(configInit.getConfig().getTcpServerIp(), configInit.getConfig().getWebTcpServerPort());
        }
    }

}
